package com.example.coursedesign.ui.notifications;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.coursedesign.R;
import com.example.coursedesign.databinding.FragmentNotificationsBinding;
import com.example.coursedesign.entity.User;

public class NotificationsFragment extends Fragment {

    private FragmentNotificationsBinding binding;
    private UserViewModel userViewModel;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        userViewModel = new ViewModelProvider(requireActivity()).get(UserViewModel.class);


        binding = FragmentNotificationsBinding.inflate(inflater, container, false);
        View root = binding.getRoot();


        // 初始化视图
        ImageView imageView = binding.avatar;
        TextView tvUsername = binding.tvUsername;
        TextView tvNickname = binding.tvNickname;
        TextView exitButton = binding.exit;

        // 使用 Glide 加载圆形图片
        Glide.with(this)
                .load(R.drawable.a) // 或者从网络加载图片
                .apply(RequestOptions.circleCropTransform())
                .into(imageView);
        // 设置初始值
        tvUsername.setText("user123");
        tvNickname.setText("快来了解中药知识吧~");


        // 获取 ImageView 的引用
        ImageView ivMonitoringArrow = binding.ivMonitoringArrow;
        // 设置点击事件
        ivMonitoringArrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 创建 Intent 并启动 FeedbackActivity
                Intent intent = new Intent(getActivity(), MonitoringActivity.class);
                startActivity(intent);
            }
        });
        // 获取 ImageView 的引用
        ImageView ivFeedbackArrow = binding.ivFeedbackArrow;


        // 设置点击事件
        ivFeedbackArrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 创建 Intent 并启动 FeedbackActivity
                Intent intent = new Intent(getActivity(), FeedbackActivity.class);
                startActivity(intent);
            }
        });
        // 获取 ImageView 的引用
        ImageView ivUserProfileArrow = binding.ivUserProfileArrow;
        // 设置点击事件
        ivUserProfileArrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 创建 Intent 并启动 FeedbackActivity
                Intent intent = new Intent(getActivity(), UserProfileActivity.class);
                startActivity(intent);
            }
        });

        ImageView ivFavoritesArrow = binding.ivFavoritesArrow;
        ivFavoritesArrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 创建 Intent 并启动 FavoritesActivity
                Intent intent = new Intent(getActivity(), CollectActivity.class);
                startActivity(intent);
            }
        });
        // 设置退出按钮点击事件
        exitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 处理退出按钮点击事件
                // 例如，退出登录或返回登录界面
//                Toast.makeText(UserProfileActivity.this, "退出登录", Toast.LENGTH_SHORT).show();
                requireActivity().finish(); // 关闭当前活动
            }
        });

        // 观察 ViewModel 中的数据变化
        userViewModel.getCurrentUser().observe(getViewLifecycleOwner(), user -> {
            if (user != null) {
                updateUserInfo(user);
            }
        });
        return root;
    }

    private void updateUserInfo(User user) {
        // 更新UI元素
        Glide.with(this)
                .load(user.getAvatar())
                .apply(RequestOptions.circleCropTransform())
                .into(binding.avatar);
        binding.tvUsername.setText(user.getName());

        // 如果还有其他地方需要更新，继续添加代码
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}